package com.zengdada.authority.service.impl;

import com.zengdada.authority.config.security.intercept.AuthorityUtil;
import com.zengdada.authority.mapper.AppPowerMapper;
import com.zengdada.authority.model.AppPower;
import com.zengdada.authority.model.AppUser;
import com.zengdada.authority.service.AppPowerAuthorityService;
import com.zengdada.authority.service.EnhanceBaseService;
import com.zengdada.authority.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2018/11/27 20:48
 * @Description:
 */
@Service
public class AppPowerAuthorityServiceImpl extends EnhanceBaseService<AppPower> implements AppPowerAuthorityService {

    @Autowired
    AuthorityUtil authorityUtil;

    @Value("${authority.application.id}")
    private String APPLICATION_ID;

    @Override
    public List<Map<String, Object>> getNavigationTree() {
        AppUser user = authorityUtil.getLoginUser(true);
        boolean isSuperAdministrator = authorityUtil.isSuperAdministrator(true);
        List<Map<String, Object>> list = CommonUtils.depthTransformHumpNew(((AppPowerMapper) mapper).selectNavMenu(user == null ? null : user.getId(), isSuperAdministrator, APPLICATION_ID));
        List<Map<String, Object>> collect = list.stream().filter(e -> "1".equals(String.valueOf(e.get("state"))) && (e.get("parentId") != null && !StringUtils.isBlank(String.valueOf(e.get("parentId")))))
                .collect(Collectors.toList());
        collect.forEach(e -> {
            StringBuilder url = new StringBuilder(String.valueOf(e.getOrDefault("href", "")));
            if (url.length() > 0 && url.charAt(0) == '/')
                url.deleteCharAt(0);
            if (url.length() > 0 && url.charAt(url.length() - 1) == '*')
                url.deleteCharAt(url.length() - 1);
            e.put("href", url.toString());
        });
        return CommonUtils.generateTreeMapCurrency(collect, "id", "parentId", "children");
    }

    @Override
    public List<Map<String, Object>> getPowerTreeByRole(String roleId) {
        boolean isSuperAdministrator = authorityUtil.isSuperAdministrator(true);
        List<Map<String, Object>> list = CommonUtils.depthTransformHumpNew(((AppPowerMapper) mapper).selectPowerByRole(roleId, isSuperAdministrator, APPLICATION_ID));
        return CommonUtils.generateTreeMapCurrency(list, "id", "parentId", "children");
    }

    @Override
    public List<Map<String, Object>> getPowerTree() {
        AppUser user = authorityUtil.getLoginUser(true);
        boolean isSuperAdministrator = authorityUtil.isSuperAdministrator(true);
        List<Map<String, Object>> list = CommonUtils.depthTransformHumpNew(((AppPowerMapper) mapper).selectNavMenu(user == null ? null : user.getId(), isSuperAdministrator, APPLICATION_ID));
        List<Map<String, Object>> collect = list.stream().filter(e -> "1".equals(String.valueOf(e.get("state")))).collect(Collectors.toList());
        return CommonUtils.generateTreeMapCurrency(collect, "id", "parentId", "children");
    }
}
